package BookFrame;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

// 图书信息管理主界面
public class JTableBooks extends JFrame {

    private DefaultTableModel tableModel;   // 表格模型对象
    private JTable table;

    JTextField bookId_textfield = new JTextField(10);
    JTextField title_textfield = new JTextField(10);

    public JTableBooks() {
        setSize(750, 400);
        // 获取屏幕的尺寸
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        // 设置程序窗口的位置为屏幕的正中央
        this.setLocation((dimension.width-getWidth())/2, (dimension.height-getHeight())/2);
        setTitle("图书信息列表");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 顶部搜索面板
        JPanel p1 = new JPanel();
        p1.add(new JLabel("请输入图书编号："));
        p1.add(bookId_textfield);
        p1.add(new JLabel("请输入图书名称："));
        p1.add(title_textfield);

        // 查询按钮
        JButton query_button = new JButton("查询—>");
        query_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String bookId = null;
                if(bookId_textfield.getText()!=null && !(bookId_textfield.getText().trim().isEmpty())){
                    bookId = bookId_textfield.getText().trim();
                }
                String title = null;
                if(title_textfield.getText()!=null && !(title_textfield.getText().trim().isEmpty())){
                    title = title_textfield.getText().trim();
                }
                tableModel.setDataVector(Dao.getInstance().queryBooksByBookIdAndTitle(bookId, title),
                        Dao.getInstance().getBookColumnNames());
            }
        });

        // 重置按钮
        JButton reset_button = new JButton("清除");
        reset_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                bookId_textfield.setText("");
                title_textfield.setText("");
                tableModel.setRowCount(0);
            }
        });

        // 显示所有记录按钮
        JButton all_button = new JButton("显示所有记录");
        all_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tableModel.setDataVector(Dao.getInstance().getBookDataByVector(),
                        Dao.getInstance().getBookColumnNames());
            }
        });

        p1.add(query_button);
        p1.add(reset_button);
        p1.add(all_button);
        getContentPane().add(p1, BorderLayout.NORTH);

        // 初始化表格模型和表格
        tableModel = new DefaultTableModel(Dao.getInstance().getBookDataByVector(),
                Dao.getInstance().getBookColumnNames());
        table = new JTable(tableModel) {	// 单元格不可编辑
            public boolean isCellEditable(int row, int column) { return false; }
        };

        JScrollPane scrollPane = new JScrollPane(table);   // 支持滚动
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        // 表格排序功能
        table.setRowSorter(new TableRowSorter<DefaultTableModel>(tableModel));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 单选

        scrollPane.setViewportView(table);

        // 底部操作按钮面板
        final JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.SOUTH);

        // 添加图书按钮
        JButton addButton = new JButton("添加");
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DetailBook bookInfo = new DetailBook();
                bookInfo.addWindowListener(new WindowAdapter() {
                    public void windowClosed(WindowEvent e) {
                        tableModel.setDataVector(Dao.getInstance().getBookDataByVector(),
                                Dao.getInstance().getBookColumnNames());
                    }
                });
                bookInfo.setVisible(true);
            }
        });
        panel.add(addButton);

        // 查看/编辑图书详情按钮
        JButton updateButton = new JButton("查看详情");
        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow();// 获得选中行的索引
                if(selectedRow != -1) {   // 是否存在选中行
                    int bookId = (Integer)table.getValueAt(selectedRow, 0);
                    Book book = Dao.getInstance().queryBookByBookId(bookId);
                    DetailBook detail = new DetailBook(book);
                    detail.setVisible(true);
                }
            }
        });
        panel.add(updateButton);

        // 删除图书按钮
        JButton delButton = new JButton("删除");
        delButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow();// 获得选中行的索引
                if(selectedRow != -1) {  // 存在选中行
                    if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null,
                            "确认要删除吗？", "请确认", JOptionPane.YES_NO_OPTION)) {
                        int bookId = (Integer)tableModel.getValueAt(selectedRow, 0);
                        Dao.getInstance().removeBookByBookId(bookId);
                        tableModel.removeRow(selectedRow);  // 删除行
                    }
                }
            }
        });
        panel.add(delButton);
    }

    public JTableBooks(BookLoginFrame logframe) {
        this();
        logframe.dispose();
        setVisible(true);
    }
}
